/* 
 * Copyright (C) 2012 Martin Řehánek <Martin.Rehanek at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


goog.provide('kramerius.audio.ui.playlist.GlobalItemUpButton');

goog.require('kramerius.audio.ui.playlist.GlobalButton');
goog.require('kramerius.audio.debug_utils');
goog.require('goog.ui.Component');
goog.require('goog.ui.Control');

/** @const */ 
var MOVE_ITEM_UP = 'playlistBtnItemUp';

/** @const */ 
var MOVE_ITEM_UP_BUTTON_STATUS_ENABLED = 'MOVE_ITEM_UP_BUTTON_ENABLED';
/** @const */ 
var MOVE_ITEM_UP_BUTTON_STATUS_DISABLED = 'MOVE_ITEM_UP_BUTTON_DISABLED';



/**
 * @constructor 
 * @param {Element} container Element containing this button.
 * @param {kramerius.audio.ui.playlist.Playlist} playlist containing this button.
 * @param {boolean} loggingEnabled whether logging should be enabled (for debugging)
 */
kramerius.audio.ui.playlist.GlobalItemUpButton = function(container, playlist, loggingEnabled){
    kramerius.audio.ui.playlist.GlobalButton.call(this, container, playlist);
    /** @private */
    //this.controller_.setPlayNextButton(this);
    /** @private */
    this.loggingEnabled_ = loggingEnabled;
    /** @private */
    this.btnControl_ = this.initializeControl_();
    this.setContainerClass_(MOVE_ITEM_UP);
    this.setStateDisabled();
};
goog.inherits(kramerius.audio.ui.playlist.GlobalItemUpButton, kramerius.audio.ui.playlist.GlobalButton);


/** @private */
kramerius.audio.ui.playlist.GlobalItemUpButton.prototype.initializeControl_ = function(){
    var control = new goog.ui.Control();
    control.setDispatchTransitionEvents(goog.ui.Component.State.ACTION, true);
    var playlist = this.playlist_;
    var button = this;
    
    goog.events.listen(control, goog.ui.Component.EventType.ACTION,
        function (e) {
            var state = button.getState();
            button.log_("ACTION, state: " + state);
            switch(state){
                case MOVE_ITEM_UP_BUTTON_STATUS_ENABLED:
                    var selectedItem = playlist.getSelectedItem();
                    if (selectedItem != undefined){
                        selectedItem.moveUpInPlaylist();
                        button.log_("moving selected item up in playlist");
                    }else{
                        var activeItem = playlist.getActiveItem();
                        if (activeItem != undefined){
                            activeItem.moveUpInPlaylist();  
                            button.log_("moving active item up in playlist");
                        } else{
                            button.log_("no selected or active item to be moved up in playlist");
                        }
                    }
                    playlist.refreshGlobaButtons();
                    break;
                case MOVE_ITEM_UP_BUTTON_STATUS_DISABLED:
                    button.log_("clicked when inactive");
                    break;
            }
        });
    control.decorate(this.container_);
    //TODO: i18n
    var tooltip = new goog.ui.Tooltip(this.container_, "posunout nahoru v seznamu skladeb");
    //tooltip.setType(goog.ui.PopupBase.Type.MOVE_OFFSCREEN);
    tooltip.setAutoHide(false);
    return control;
};


kramerius.audio.ui.playlist.GlobalItemUpButton.prototype.setStateDisabled = function(){
    this.setState_(MOVE_ITEM_UP_BUTTON_STATUS_DISABLED);
};


kramerius.audio.ui.playlist.GlobalItemUpButton.prototype.setStateEnabled = function(){
    this.setState_(MOVE_ITEM_UP_BUTTON_STATUS_ENABLED);
};


/** @private */
kramerius.audio.ui.playlist.GlobalItemUpButton.prototype.setState_ = function(state){
    this.log_("setting state to " + state);
    this.state_ = state;
};

kramerius.audio.ui.playlist.GlobalItemUpButton.prototype.getState = function(){
    return this.state_;
};

/** @private */
kramerius.audio.ui.playlist.GlobalItemUpButton.prototype.log_= function(text){
    if(this.loggingEnabled_){
        console.log("ITEM UP button: " + text);
    }
}